package com.bear.newsset.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.bear.newsset.dao.entity.StringMapSqlEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "STRING_MAP_SQL_ENTITY".
*/
public class StringMapSqlEntityDao extends AbstractDao<StringMapSqlEntity, Long> {

    public static final String TABLENAME = "STRING_MAP_SQL_ENTITY";

    /**
     * Properties of entity StringMapSqlEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property OnlyId = new Property(1, String.class, "onlyId", false, "ONLY_ID");
        public final static Property Key = new Property(2, String.class, "key", false, "KEY");
        public final static Property Val = new Property(3, String.class, "val", false, "VAL");
    }

    private Query<StringMapSqlEntity> newsDataSqlEntity_EntitiesListQuery;

    public StringMapSqlEntityDao(DaoConfig config) {
        super(config);
    }
    
    public StringMapSqlEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"STRING_MAP_SQL_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"ONLY_ID\" TEXT UNIQUE ," + // 1: onlyId
                "\"KEY\" TEXT," + // 2: key
                "\"VAL\" TEXT);"); // 3: val
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"STRING_MAP_SQL_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, StringMapSqlEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String onlyId = entity.getOnlyId();
        if (onlyId != null) {
            stmt.bindString(2, onlyId);
        }
 
        String key = entity.getKey();
        if (key != null) {
            stmt.bindString(3, key);
        }
 
        String val = entity.getVal();
        if (val != null) {
            stmt.bindString(4, val);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, StringMapSqlEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String onlyId = entity.getOnlyId();
        if (onlyId != null) {
            stmt.bindString(2, onlyId);
        }
 
        String key = entity.getKey();
        if (key != null) {
            stmt.bindString(3, key);
        }
 
        String val = entity.getVal();
        if (val != null) {
            stmt.bindString(4, val);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public StringMapSqlEntity readEntity(Cursor cursor, int offset) {
        StringMapSqlEntity entity = new StringMapSqlEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // onlyId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // key
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3) // val
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, StringMapSqlEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setOnlyId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setKey(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setVal(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(StringMapSqlEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(StringMapSqlEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(StringMapSqlEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "entitiesList" to-many relationship of NewsDataSqlEntity. */
    public List<StringMapSqlEntity> _queryNewsDataSqlEntity_EntitiesList(String onlyId) {
        synchronized (this) {
            if (newsDataSqlEntity_EntitiesListQuery == null) {
                QueryBuilder<StringMapSqlEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.OnlyId.eq(null));
                newsDataSqlEntity_EntitiesListQuery = queryBuilder.build();
            }
        }
        Query<StringMapSqlEntity> query = newsDataSqlEntity_EntitiesListQuery.forCurrentThread();
        query.setParameter(0, onlyId);
        return query.list();
    }

}
